# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def postorderTraversal(self, root):
        # tmp = []
        # def dfs(root):
        #     if not root: return
        #     dfs(root.left)
        #     dfs(root.right)
        #     tmp.append(root.val)
        # dfs(root)
        # return tmp
        White, Gray = 0, 1
        tmp = []
        stack = [[White, root]]
        while stack:
            color, node = stack.pop()
            if not node: continue
            if color == White:
                stack.append([Gray, node])
                stack.append([White, node.right])
                stack.append([White, node.left])
            else:
                tmp.append(node.val)
        return tmp
